package com.foreveross.bsl.mam.app.web;

import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.bsl.mam.app.application.AppSecretApplication;
import com.foreveross.bsl.mam.app.application.DefindApplication;
import com.foreveross.bsl.mam.app.application.vo.AppSecretVo;
import com.foreveross.bsl.mam.app.application.vo.DefindVo;

/**
 * 个性化设置类
 * 
 * @author tanzhixiong
 * @2013-6-22 @下午3:01:42
 */
@Controller
@RequestMapping("/mam/logos")
public class LogoCountroller extends AppBaseController {

	@Inject
	private DefindApplication defindApplication;

	@Inject
	private AppSecretApplication appSecretApplication;

	/**
	 * 进入logo个性化设置
	 * 
	 * @param appMainVo
	 * @return
	 */
	@RequestMapping(value = "/{platform}/{version}/{appId}", method = RequestMethod.GET)
	public String enterLogo(@PathVariable("appId") String appId,
			@PathVariable("platform") String platform,
			@PathVariable("version") String version, Model model) {
		DefindVo defindVo = defindApplication.getEntityByAppId(platform,
				version, appId);
		model.addAttribute("platform", platform);
		model.addAttribute("version", version);
		model.addAttribute("appId", appId);
		model.addAttribute("defind", defindVo);
		return "app/defind-logo";
	}

	/**
	 * 保存信息
	 * 
	 * @param defindVo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{platform}/{version}/{appId}", method = RequestMethod.POST)
	public Map<String, Object> saveLogo(
			@PathVariable("platform") String platform,
			@PathVariable("version") String version,
			@PathVariable("appId") String appId, DefindVo defindVo) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DefindVo vo = defindApplication.getEntityByAppId(platform, version,
					appId);
			if (vo != null) { // 已经存在，则进行更新
				defindVo.setId(vo.getId());
				vo = defindApplication.saveEntity(defindVo);
			} else {
				vo = defindApplication.saveEntity(defindVo);
			}
			result.put("result", "success");
			result.put("id", vo.getId());
		} catch (Exception e) {
			e.printStackTrace();
			result.put("result", "error");
		}
		return result;
	}

	/**
	 * 进入打包页面
	 * 
	 * @param appId
	 * @param platform
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{platform}", method = RequestMethod.GET)
	public String enterPack(@PathVariable("platform") String platform,
			@RequestParam("appId") String appId,
			@RequestParam("version") String version, Model model) {
		AppSecretVo secretVo = appSecretApplication
				.findByAppId(appId, platform);
		if (secretVo != null) {
			model.addAttribute("secret", "true");
		}
		model.addAttribute("platform", platform);
		model.addAttribute("appId", appId);
		model.addAttribute("version", version);
		return "app/app-pack";
	}

}
